package com.coolzeng.filter;

import com.coolzeng.entity.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2017/6/9.
 */
public class LoginFilter implements javax.servlet.Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain filterChain) throws IOException,ServletException {

        HttpServletRequest hsq = (HttpServletRequest) req;
        User u = (User) hsq.getSession().getAttribute("loginUser");
        if (u == null) {
            ((HttpServletResponse) resp).sendRedirect(hsq.getContextPath() + "/login.jsp");
        }
        filterChain.doFilter(req,resp);

    }

    @Override
    public void destroy() {

    }
}
